<template>
  <div>
    <div v-for="(item,index) in uploadList" :key="index" class="upload-list">
      <template v-if="item.status === 'finished'">
        <img :src="item.url">
        <div class="upload-list-cover">
          <Icon type="ios-eye-outline" @click="handleView(item.url)"></Icon>
          <Icon type="ios-trash-outline" @click="handleRemove(item)"></Icon>
        </div>
      </template>
      <template v-else>
        <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
      </template>
    </div>
    <Upload
      ref="upload"
      :action="uploadFileUrl"
      :before-upload="handleBeforeUpload"
      :data="params"
      :format="['jpg','jpeg','png']"
      :max-size="2*1024"
      :on-error="handleError"
      :on-exceeded-size="handleMaxSize"
      :on-format-error="handleFormatError"
      :on-success="handleSuccess"
      :show-upload-list="false"
      accept=".jpg, .jpeg, .png"
      style="display: inline-block;"
      type="drag"
    >
      <div v-show="showDrag" style="width: 158px;height:100px;line-height: 100px;">
        <Icon size="20" type="md-add"></Icon>
      </div>
    </Upload>

    <Modal v-model="viewImage" :styles="{top: '30px'}" draggable title="图片预览">
      <img :src="imgUrl" alt="无效的图片链接" style="width: 100%;margin: 0 auto;display: block;">
      <div slot="footer">
        <Button @click="viewImage=false">关闭</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import Viewer from 'viewerjs'
import {uploadFile} from '../../api/upload'

export default {
  name: 'UploadFileThumb',
  props: {
    multiple: {
      type: Boolean,
      default: false
    },
    params: {
      type: Object
    }
  },
  data() {
    return {
      showDrag: true,
      uploadFileUrl: uploadFile,
      uploadList: [],
      viewImage: false,
      imgUrl: ''
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    handleBeforeUpload() {
      return true
    },
    handleClearFiles() {
      this.uploadList = []
    },
    handleError(error, file, fileList) {
      this.$Message.error(error.toString())
    },
    handleFormatError(file) {
      this.$Notice.warning({
        title: '不支持的文件格式',
        desc:
          '所选文件‘ ' +
          file.name +
          ' ’格式不正确, 请选择 .jpg .jpeg .png 图片格式文件'
      })
    },
    handleMaxSize(file) {
      this.$Notice.warning({
        title: '文件大小过大',
        desc: '所选文件大小过大, 不得超过 2M.'
      })
    },
    handleRemove(file) {
      const uploadList = this.uploadList
      this.uploadList.splice(uploadList.indexOf(file), 1)
      this.returnValue()
      this.showDrag = true
    },
    handleSuccess(res, file) {
      if (res.data.success) {
        this.showDrag = false
        file.url = res.data.url
        // 单张图片处理
        if (!this.multiple && this.uploadList.length > 0) {
          // 删除第一张
          this.uploadList.splice(0, 1)
        }
        this.uploadList.push(file)
        // 返回组件值
        this.returnValue()
        this.$Message.success(res.message)
      } else {
        this.$Message.error(res.message)
      }
    },
    handleView(v) {
      const image = new Image()
      image.src = v
      const viewer = new Viewer(image, {
        hidden: function () {
          viewer.destroy()
        }
      })
      viewer.show()
    },
    // handleView(imgUrl) {
    //   this.imgUrl = imgUrl
    //   this.viewImage = true
    // },
    init() {
    },
    returnValue() {
      if (!this.uploadList || this.uploadList.length < 1) {
        if (!this.multiple) {
          this.$emit('on-change', '')
        } else {
          this.$emit('on-change', [])
        }
        return
      }
      if (!this.multiple) {
        // 单张
        const v = this.uploadList[0].url
        this.$emit('on-change', v)
      } else {
        const v = []
        this.uploadList.forEach(e => {
          v.push(e.url)
        })
        this.$emit('on-change', v)
      }
    },
    setData(v) {
      if (v === null) {
        this.showDrag = true
        this.uploadList = []
        return
      }
      this.showDrag = false
      if (typeof v == 'string') {
        const item = {
          url: v,
          status: 'finished'
        }
        this.uploadList.push(item)
      } else if (typeof v == 'object') {
        v.forEach(e => {
          const item = {
            url: e,
            status: 'finished'
          }
          this.uploadList.push(item)
        })
      }
      this.returnValue()
    }
  }
}
</script>

<style lang="less">
.upload-list {
  display: inline-block;
  width: 158px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 5px;
}

.upload-list img {
  width: 100%;
  height: 100%;
}

.upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}

.upload-list:hover .upload-list-cover {
  display: block;
}

.upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}
</style>

